package cn.xinfei.xdecision.manager.controller.component.rulebase;

import cn.xinfei.xdecision.common.model.common.ResponseEntityBuilder;
import cn.xinfei.xdecision.common.model.common.ResponseEntityDto;
import cn.xinfei.xdecision.common.model.common.requestParam.UpdateStatusParam;
import cn.xinfei.xdecision.common.model.component.CopyToFolderReqVo;
import cn.xinfei.xdecision.common.model.component.guiderule.vo.RuleListReqVo;
import cn.xinfei.xdecision.common.model.component.guiderule.vo.RuleVo;
import cn.xinfei.xdecision.common.service.component.guiderule.RuleService;
import cn.xinfei.xdecision.common.utils.constant.OperateNameConst;
import cn.xinfei.xdecision.common.utils.constant.OperateTypeEnum;
import cn.xinfei.xdecision.common.utils.enums.enginex.ComponentStatusEnum;
import cn.xinfei.xdecision.common.utils.exception.ApiException;
import cn.xinfei.xdecision.common.utils.exception.enums.ErrorCodeEnum;
import cn.xinfei.xdecision.manager.web.aop.ArchivesLog;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;

@Api(tags = "组件中心-规则库")
@RestController
@RequestMapping("v3/rule")
public class RuleInfoController {

    @Autowired
    private RuleService ruleService;

	@ApiOperation(value = "规则详情")
	@PostMapping(value = "getRuleInfo/{id}")
	public ResponseEntityDto<RuleVo> getRuleInfo(@PathVariable Long id) {
		if (id == null) {
			throw new ApiException(ErrorCodeEnum.PARAMS_EXCEPTION.getCode(), ErrorCodeEnum.PARAMS_EXCEPTION.getMessage());
		}
		return ResponseEntityDto.success(ruleService.queryById(id));
	}

    @ResponseBody
    @RequestMapping(value = "getRuleList", method = RequestMethod.POST)
	@ApiOperation(value = "规则列表", notes = "规则列表")
    public ResponseEntityDto getRuleList(@RequestBody @Valid RuleListReqVo ruleListReqVo) {
        PageInfo pageInfo = ruleService.getRuleList(ruleListReqVo);
        HashMap<String, Object> responseMap = new HashMap<>();
        responseMap.put("klist", pageInfo.getList());
        pageInfo.setList(null);
        responseMap.put("pageInfo", pageInfo);
        return ResponseEntityBuilder.buildNormalResponse(responseMap);
    }

    @ResponseBody
    @RequestMapping(value = "addRule", method = RequestMethod.POST)
    @ApiOperation(value = "新增规则", notes = "新增规则")
    @ArchivesLog(type = OperateTypeEnum.CREATE,name = OperateNameConst.ComponentOpt.SAVE)
    public ResponseEntityDto<RuleVo> addRule(@RequestBody RuleVo ruleVo) {
        RuleVo insert = ruleService.insertRuleInfo(ruleVo);
        return ResponseEntityBuilder.buildNormalResponse(insert);
    }

    @RequestMapping(value = "updateRule", method = RequestMethod.POST)
	@ApiOperation(value = "修改规则", notes = "修改规则")
    @ArchivesLog(type = OperateTypeEnum.UPDATE,name = OperateNameConst.ComponentOpt.UPDATE)
    public ResponseEntityDto<RuleVo> updateRule(@RequestBody RuleVo rule) {
        RuleVo update = ruleService.updateRuleInfo(rule);
        return ResponseEntityBuilder.buildNormalResponse(update);
    }

    @ResponseBody
    @RequestMapping(value = "enable", method = RequestMethod.POST)
	@ApiOperation(value = "启用规则", notes = "启用规则")
    @ArchivesLog(type = OperateTypeEnum.UPDATE,name = OperateNameConst.ComponentOpt.ENABLE)
    public ResponseEntityDto enable(@RequestBody UpdateStatusParam updateStatusReqVo) {
		updateStatusReqVo.setStatus(ComponentStatusEnum.ENABLE.getStatus());
        boolean updateResult = ruleService.updateStatus(updateStatusReqVo);
        return ResponseEntityBuilder.buildNormalResponse(updateResult);
    }

	@ResponseBody
	@RequestMapping(value = "disable", method = RequestMethod.POST)
	@ApiOperation(value = "停用规则", notes = "停用规则")
	@ArchivesLog(type = OperateTypeEnum.UPDATE,name = OperateNameConst.ComponentOpt.DISABLE)
	public ResponseEntityDto disable(@RequestBody UpdateStatusParam updateStatusReqVo) {
		updateStatusReqVo.setStatus(ComponentStatusEnum.DISABLE.getStatus());
		boolean updateResult = ruleService.updateStatus(updateStatusReqVo);
		return ResponseEntityBuilder.buildNormalResponse(updateResult);
	}


	@ResponseBody
	@RequestMapping(value = "copyRule", method = RequestMethod.POST)
	@ApiOperation(value = "复制规则", notes = "复制规则")
	@ArchivesLog(type = OperateTypeEnum.CREATE,name = OperateNameConst.ComponentOpt.COPY)
	public ResponseEntityDto<String> updateParent(@RequestBody @Valid CopyToFolderReqVo copyToFolderReqVo) {
        ruleService.copyRule(copyToFolderReqVo);
		return ResponseEntityBuilder.buildNormalResponse("复制规则成功!");
	}




}
